package cn.patient.pp.module.patients.controller.admin;

import cn.patient.pp.framework.excel.core.listener.TemplateCheckListener;
import cn.patient.pp.framework.security.core.LoginUser;
import cn.patient.pp.framework.security.core.util.SecurityFrameworkUtils;
import cn.patient.pp.module.patients.controller.admin.vo.*;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.ImportRespVO;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.PatientsDOExeclVO;
import cn.patient.pp.module.patients.framework.web.config.Config;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.patient.pp.framework.common.pojo.PageParam;
import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.common.pojo.CommonResult;
import cn.patient.pp.framework.common.util.object.BeanUtils;
import static cn.patient.pp.framework.common.pojo.CommonResult.success;

import cn.patient.pp.framework.excel.core.util.ExcelUtils;

import cn.patient.pp.framework.apilog.core.annotation.ApiAccessLog;
import static cn.patient.pp.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.patient.pp.module.patients.dal.dataobject.patients.PatientsDODO;
import cn.patient.pp.module.patients.service.PatientsDOService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "基本信息管理 - 患者总")
@RestController
@RequestMapping("/business/patients")
@Validated
public class PatientsDOController {

    @Resource
    private PatientsDOService dOService;

//    @PostMapping("/create")
//    @Operation(summary = "创建患者总")
//    @ApiAccessLog(operateType = CREATE)
//    @PreAuthorize("@ss.hasPermission('patients:create')")
//    public CommonResult<Integer> createDO(@Valid @RequestBody PatientsDOSaveReqVO createReqVO) {
//        return success(dOService.createDO(createReqVO));
//    }

//    @PutMapping("/update")
//    @Operation(summary = "更新患者总")
//    @ApiAccessLog(operateType = UPDATE)
//    @PreAuthorize("@ss.hasPermission('patients:update')")
//    public CommonResult<Boolean> updateDO(@Valid @RequestBody PatientsDOSaveReqVO updateReqVO) {
//        dOService.updateDO(updateReqVO);
//        return success(true);
//    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除患者总")
//    @ApiAccessLog(operateType = DELETE)
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('patients:delete')")
//    public CommonResult<Boolean> deleteDO(@RequestParam("id") Integer id) {
//        dOService.deleteDO(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得患者基本信息详情")
    @ApiAccessLog(operateType = GET)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('patient:baseInfo:details')")
    public CommonResult<PatientsDORespVO> getDO(@RequestParam("id") Integer id) {
        PatientsDODO dO = dOService.getDO(id);
        return success(BeanUtils.toBean(dO, PatientsDORespVO.class));
    }

    @GetMapping("/getByPatientCode")
    @Operation(summary = "通过患者编号获得患者基本信息详情")
    @ApiAccessLog(operateType = GET)
    @Parameter(name = "patientCode", description = "患者编号", required = true)
    @PreAuthorize("@ss.hasPermission('patients:alarmInfo:detail')")
    public CommonResult<PatientsDORespVO> getByPatientCode(@RequestParam("patientCode") String patientCode) {
        PatientsDODO dO = dOService.getByPatientCode(patientCode);
        return success(BeanUtils.toBean(dO, PatientsDORespVO.class));
    }

    @GetMapping("/getByReportPatientInfoId")
    @Operation(summary = "通过新筛查患者id获得患者基本信息详情")
    @ApiAccessLog(operateType = GET)
    @Parameter(name = "id", description = "新筛查患者id", required = true)
    @PreAuthorize("@ss.hasPermission('patients:alarmInfo:detail')")
    public CommonResult<PatientsDORespVO> getByReportPatientInfoId(@RequestParam("id") Long id) {
        PatientsDODO dO = dOService.getByReportPatientInfoId(id);
        return success(BeanUtils.toBean(dO, PatientsDORespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得患者总分页")
    @ApiAccessLog(operateType = GET)
    @PreAuthorize("@ss.hasPermission('patients:baseInfo:query')")
    public CommonResult<PageResult<PatientsDORespVO>> getDOPage(@RequestBody @Valid PatientsDOPageReqVO pageReqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        PageResult<PatientsDODO> pageResult = dOService.getDOPage(pageReqVO, loginUser.getId());
        return success(BeanUtils.toBean(pageResult, PatientsDORespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出患者Excel模板")
    @PreAuthorize("@ss.hasPermission('patients:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDOExcel(@Valid PatientsDOPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
//        List<PatientsDODO> list = dOService.getDOPage(pageReqVO, loginUser.getId()).getList();
        ArrayList<PatientsDOExeclVO> list = new ArrayList<>();
        // 导出 Excel
        ExcelUtils.write(response, "患者总.xls", "数据", PatientsDOExeclVO.class,
                        BeanUtils.toBean(list, PatientsDOExeclVO.class));
    }

    @PostMapping("/import-basicinfo")
    @Operation(summary = "导入病人基本数据")
    @ApiAccessLog(operateType = IMPORT)
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('patients:baseinfo:import')")
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        TemplateCheckListener<PatientsDOExeclVO> listener = new TemplateCheckListener<>(Config.PATIENT_BASIC_INFO);
        List<PatientsDOExeclVO> list = ExcelUtils.read(file, PatientsDOExeclVO.class,listener);
        return success(dOService.importBaseInfoList(list, updateSupport));
    }
}